<?php

namespace app\admin\model;

class CouponUsed extends \app\common\model\CouponUsed
{
    /**
     * 获取已使用的优惠券
     * @param $oauthId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getColumn($oauthId)
    {
        $model = static::field('coupon_id, order_sn, create_time')
            ->where(['oauth_id' => $oauthId])
            ->select()
            ->toArray();

        $result = [];
        foreach ($model as $key => $value) {
            $result[$value['coupon_id']] = [
                'order_sn' => $value['order_sn'],
                'create_time' => $value['create_time']
            ];
        }
        return $result;
    }

    /**
     * 获取已使用的优惠券
     * @param $couponIds
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getColumnByCoupon($couponIds)
    {
        return static::field('coupon_id, COUNT(*) AS count')
            ->where('coupon_id', 'in', $couponIds)
            ->group('coupon_id')
            ->select()
            ->column('count', 'coupon_id');
    }
}